package com.iap.client.mediator
{
	import com.iap.admin.event.TagEvent;
	import com.iap.app.AppGlobal;
	import com.iap.app.AppText;
	import com.iap.client.event.FavoriteRendererEvent;
	import com.iap.client.model.FavoriteDisplayModel;
	import com.iap.client.view.favorite.FavoriteView;
	import com.iap.common.AppSession;
	import com.iap.common.event.ClientEvent;
	import com.iap.common.event.FavoriteEvent;
	import com.iap.common.event.InstanceEvent;
	import com.iap.common.mediator.BaseMediator;
	import com.iap.common.model.Client;
	import com.iap.common.model.ClientLite;
	import com.iap.common.model.Favorite;
	import com.iap.common.model.Instance;
	import com.iap.common.model.Tag;
	import com.iap.common.signal.ClientLiteRequestSignal;
	import com.iap.common.signal.ClientRequestSignal;
	import com.iap.common.signal.FavoriteRequestSignal;
	import com.iap.common.signal.InstanceRequestSignal;
	import com.iap.common.signal.TagRequestSignal;
	
	import flash.events.Event;
	
	import mx.collections.ArrayList;
	import mx.controls.Alert;
	import mx.core.mx_internal;
	import mx.events.CloseEvent;
	
	public class FavoriteMediator extends BaseMediator
	{
		[Inject]
		public var view:FavoriteView;
		[Inject]
		public var request:FavoriteRequestSignal;
		[Inject]
		public var tagRequest:TagRequestSignal;
		private var tagLoaded:Boolean = false;
		[Inject]
		public var clientRequest:ClientLiteRequestSignal;
		private var clientLoaded:Boolean = false;
		[Inject]
		public var instanceRequest:InstanceRequestSignal;

		override public function onRegister():void
		{
			addContextListener(FavoriteEvent.RESPONSE, responseHandler);
			addContextListener(TagEvent.RESPONSE, tagResponseHandler);
			addContextListener(InstanceEvent.RESPONSE, instanceResponseHandler);
			view.addEventListener(FavoriteRendererEvent.DELETE, deleteFavoriteHandler);
			view.addEventListener(FavoriteView.EVENT_UPDATE, updateFavoriteHandler);
			dispatchTagRequest();
		}
		
		private function updateFavoriteHandler(evt:Event = null):void
		{
			if(AppSession.currentInstance == null) return;
			var currentInstance:Instance = AppSession.currentInstance;
			instanceRequest.dispatch(AppSession.currentInstance, InstanceRequestSignal.GET);
		}

		private function instanceResponseHandler(evt:InstanceEvent):void
		{
			var instance:Instance = evt.response.result as Instance;
			if(instance.id > 0)
				AppSession.currentInstance = instance;
			var currentInstance:Instance = AppSession.currentInstance;
			var favorites:Array = AppSession.currentInstance.favorites;
			view.dataProvider = setFavoriteDisplayData(favorites);
		}

		private function dispatchTagRequest():void
		{
			if(tagLoaded) return;
			AppGlobal.startWait();
			tagRequest.dispatch(new Tag(), TagRequestSignal.GET);
			tagLoaded = true;
		}

		private function dispatchClientRequest():void
		{
			if(clientLoaded) return;
			AppGlobal.startWait();
			clientRequest.dispatch(new ClientLite(), ClientLiteRequestSignal.GET);
			clientLoaded = true;
		}

		private function responseHandler(evt:FavoriteEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.task == FavoriteRequestSignal.DELETE)
			{
				updateFavoriteHandler();
			}
		}
		
		private function tagResponseHandler(evt:TagEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.task == TagRequestSignal.GET)
			{
				dispatchClientRequest();
			}
		}
		
		private function setFavoriteDisplayData(list:Array):ArrayList
		{
			var result:ArrayList = new ArrayList();
			if(list == null) return result;
			for(var i:uint = 0; i<list.length; i++)
			{
				var model:FavoriteDisplayModel = new FavoriteDisplayModel();
				model.order = (i+1);
				model.favorite = list[i];
				result.addItem(model);
			}
			return result;
		}

		private function deleteFavoriteHandler(evt:FavoriteRendererEvent):void
		{
			showConfirm(function():void{
				request.dispatch(evt.data, FavoriteRequestSignal.DELETE);
			});
		}

		private function showConfirm(action:Function):void
		{
			var alert:Alert = Alert.show(AppText.CONFIRM_DELETE, AppText.CONFIRM_DELETE_TITLE, Alert.YES|Alert.NO, null, null, null, Alert.NO);
			var closeFnc:Function;
			alert.addEventListener(CloseEvent.CLOSE, closeFnc = function(evt:CloseEvent):void{
				alert.removeEventListener(CloseEvent.CLOSE, closeFnc);				
				if (evt.detail == Alert.YES)
				{
					action();
				}
			});
		}
	}
}